def calendar
end

def calendar_calendar
	mtimes = {}
	@file_extension.each {|fext|
		Dir.glob("#{@datadir}/**/*.#{fext}").each {|fp|
			mtimes[File.mtime(fp).strftime("%Y%m%d")] = 1
		}
	}

	if @options.has_year? && @options.has_month?
		time = Time.local(@options.year, @options.month, 1)
	elsif @options.has_year?
		time = Time.local(@options.year, 1, 1)
	else
		time = Time.now
	end

	generate(time, mtimes)
end

def generate(date, mtimes)
	days = []
	day = Time.local(date.year, date.month, 1)

	while day.month == date.month
		days << day
		day += 24*60*60
	end

	makehtml(date, mtimes, days)
end

def makehtml(date, mtimes, days)
	_ = makecaption(date)
	_ << %Q(<table><tr>)
	# _ << calendar_header() + '<tr>'
	days.first.wday.times { _ << '<td></td>' }
	days.each_with_index {|d, i|
		dc = dayclass(d.wday)
		_ << '<tr>' if d.wday == 0

		if mtimes.has_key?(d.strftime("%Y%m%d"))
			_ << %Q(<td#{dc}><a href="#{@url}/#{d.strftime("%Y/%m/%d/")}">#{i+1}</a></td>)
		else
			_ << "<td#{dc}>#{i+1}</td>"
		end

		_ << "</tr>\n" if d.wday == 6
	}
	(6 - days.last.wday).times { _ << '<td>&nbsp;</td>' }
	_ << "</tr>\n" unless days.last.wday == 6
	_ << %Q(</table>)

	_
end

def makecaption(date)
	_ = %Q(<a href="#{@url}/#{date.strftime("%Y/%m/")}">#{date.strftime("%B")}</a>)
	%Q(<p id="caption">) + beforeafter(date, _) + "</p>\n"
end

def dayclass(wday)
	case wday
	when 0
		' class="sunday"'
	when 6
		' class="saturday"'
	else
		''
	end
end

def beforeafter(date, center)
	day = Time.local(date.year, date.month, 1)

	mtimes = []
	@file_extension.each {|fext|
		Dir.glob("#{@datadir}/**/*.#{fext}").each {|f|
			m = File.mtime(f)
			mtimes << Time.local(m.year, m.month, 1)
		}
	}
	mtimes.uniq!
	mtimes.sort!
	idx = mtimes.index(day)
	unless idx
		mtimes << day
		mtimes.uniq!
		mtimes.sort!
		idx = mtimes.index(day)
	end
	  
	_ = ''
	if 0 < idx
		pm = mtimes[idx - 1]
		_ << %Q(<a href="#{@url}/#{pm.strftime("%Y/%m/")}">&laquo;</a> )
	else
		_ << '&nbsp;&nbsp;'
	end
	_ << center
	if mtimes.size > idx + 1
		pm = mtimes[idx + 1]
		_ << %Q( <a href="#{@url}/#{pm.strftime("%Y/%m/")}">&raquo;</a>)
	else
		_ << '&nbsp;&nbsp;'
	end

	_
end

# not use
def calendar_header()
	'<th>' + Time::RFC2822_DAY_NAME.join('</th><th>') + "</th>\n"
end
